<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
  import * as echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from './mixins/resize'
  export default {
    name: 'BenefitsBarChart',
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '300px'
      }
    },
    watch: {
      benefitsSalaryData: {
        deep: true,
        handler(val) {
          this.setOptions(val)
        }
      }
    },
    data() {
      return {
        benefitsSalaryData: [],
        chart: null
      };
    },
    mounted() {
      this.getBenefitsYearBar();
      this.$nextTick(() => {
        this.initChart();
      })
    },
    methods: {
      getBenefitsYearBar() {
        this.getRequest('/salary/analyze/benefits-year-bar').then(resp => {
          if (resp) {
            this.benefitsSalaryData = resp.data;
          }
        })
      },
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        this.setOptions(this.benefitsSalaryData);
      },
      setOptions(data = []) {
        let xAxis = [];
        let series = []
        data.forEach(e => {
          xAxis.push(e.year + '');
          series.push(e.sumBenefits);
        })
        this.chart.clear();
        this.chart.setOption({
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            },
            textStyle: {
              color: '#fff'
            }
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: xAxis,
              axisTick: {
                alignWithLabel: true
              }
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: '节日福利年度统计',
              itemStyle: {
                normal: {
                  color: '#5ab1ef',
                  lineStyle: {
                    color: '#5ab1ef',
                    width: 2
                  }
                }
              },
              type: 'bar',
              barWidth: '35%',
              data: series
            }
          ]
        });
      }
    }
  }
</script>

<style lang='scss' scoped>

</style>